package org.emet.dao.storage;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.emet.entity.storage.PurchaseHist;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.arch.ArchDataSourceBaseDao;
import org.emet.service.dto.storage.PurchaseCriteria;
import org.springframework.stereotype.Repository;

@Repository
public class PurchaseHistDao extends ArchDataSourceBaseDao<PurchaseHist, Long> {
	private static String entityClassName = PurchaseHist.class.getSimpleName();

	public Page<PurchaseHist> search(Page<PurchaseHist> page, PurchaseCriteria c) {
		String hql = "from " + entityClassName + " where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (c.getStart() != null) {
			hql += " and date >= :startDate";
			params.put("startDate", c.getStart());
		}
		if (c.getEnd() != null) {
			hql += " and date <= :endDate";
			params.put("endDate", c.getEnd());
		}
		if (!StringUtils.isBlank(c.getFlowNo())) {
			hql += " and flowNo = :flowNo";
			params.put("flowNo", c.getFlowNo());
		}

		hql += " order by date desc";
		return findPage(page, hql, params);
	}

}
